var InterfaceId;
var InterfaceShceduingName;
var setData = [{
					id:'',
					name:'全部'
				},{
					id:'unSet',
					name:'未设置'
				},{
					id:'set',
					name:'已设置'
			 }];
$(document).ready(function(){
	$('#win').window('close');
	
	$(".button_con a" ).removeClass("l-btn-plain");
	
	$("#toolbar_add").click(function(e) {
		 $('#addForm')[0].reset();
		 $("body").css("overflow-x","hidden");
	});
	
	$(".panel-tool-close, #cancalBtn").click(function(e) {
		$("body").css("overflow-x","inherit")
	});

		//所属领域下拉框
		$('#comboxSearchfieldName').combobox({
			url: BASE_URL_FIELD_SEARCH + '/findAllOfCurrentUserCanManage' + '?v=' + Math.random(),
			valueField: 'id',
		    textField: 'name',
		    editable:false,
		    method:'get',
		    loadFilter: function(data){
		    	var allCheck = {'id':'','name':'全部'};
		    	data.content.unshift(allCheck);
		    	return data.content;
		    },
		    onSelect: function(record){
		    	comboxSearchSystemName.combobox({
                    disabled: false,
                    method:'get',
                    queryParams: {fieldId: record.id},
                    url: BASE_URL_FIELD_SYSTEM_SEARCH +'/findByFieldOfCurrentUserCanManage' + '?v=' + Math.random(),
                    valueField: 'id',
                    textField: 'name',
					data:'',
        		    loadFilter: function(data){
        		    	var allCheck = {'id':'','name':'全部'};
        		    	data.content.unshift(allCheck);
        		    	return data.content;
        		    },
        		    onLoadSuccess:function(){
        		    	comboxSearchSystemName.combobox('setValue','');
        		    }
                }).load();
		    },
		    onLoadSuccess:function(){
		    	$('#comboxSearchfieldName').combobox('setValue','');
		    }
		});
		
		//所属系统下拉框
		var comboxSearchSystemName = $('#comboxSearchSystemName').combobox({
            disabled: true,
            valueField: 'id',
            textField: 'name',
            editable:false,
		  data:[{id:'',name:'全部','selected':'true'}]
        });
		
		$("#comboxSearchServerForDisplay").combobox({
			data:setData,
			valueField: 'id',
	        textField: 'name',
			editable:false
		})
		//执行接口服务设置表格
		$("#datagrid").datagrid({
			url : BASE_URL_INTERFACE_SCHEDULE_SEARCH + '/findByExample' + '?v=' + Math.random(),
			method:'get',
			autoRowHeight:true,
			singleSelect:true,
			striped:true,
			nowrap:false,
			pageSize:20,
			pageList:[10,20,50,100],
			columns:[[
						{field:'fieldName',title:'所属领域',width:100},
						{field:'fieldSystemName',title:'所属系统',width:100},
						{field:'name',title:'接口调度名',width:100,align:'left'},
						{field:'interfaceName',title:'接口名称',width:100,align:'left'},
						{field:'serverForDisplay',title:'执行服务器',width:70,
							formatter: function(value,row,index){
										//alert("id:"+row.id);
								if(getParamobjFromUrl(window.location.href).model === "view")
								{
									if(value != null){
										return value;	
									}else{
										return '未设置';	
									}
								}else
								{
									if(value != null){
										return '<a href="#" class="default_btn" onclick="showWin(\'' + index+ '\')">' + value + '</a>';	
									}else{
										return '<a href="#" class="default_btn" onclick="showWin(\'' + index+ '\')">未设置</a>';	
									}
								}
							} 
						},
						{field:'remark',title:'备注',width:100,align:'left'}
			      ]],
			pagination:true,
			onSelect:function(rowIndex,rowData){
				$("#toolbar_edit").linkbutton('enable');
				$("#toolbar_delete").linkbutton('enable');
				$(this).datagrid('unselectRow', rowIndex);
			},
			loadFilter:function(data){
				return {
					total: data.totalElements,
					rows: data.content
				};
			},
			onBeforeLoad:function(param){
				param.page -= 1;
				param.size = param.rows;
				delete param.rows;
				
				
				param.sort = 'createTime,desc';
				var fieldId = $('#comboxSearchfieldName').combobox('getValue');
				if(fieldId != ""){
					param.fieldId = fieldId;
				}
				
				var fieldSystemId = $('#comboxSearchSystemName').combobox('getValue');
				if(fieldSystemId !=""){
					param.fieldSystemId = fieldSystemId;
				}
				var interfaceName = $('#txtSearchInterfaceName').val();
				if(interfaceName != ""){
					param.interfaceName = interfaceName;
				}
				var ifSet = $('#comboxSearchServerForDisplay').combobox('getValue');
				if(ifSet != ""){
					param.ifSet = ifSet;
				}
			}
		});
		
		$("#datagrid").datagrid('getPager').pagination({   
		    displayMsg:'当前显示从 [{from}] 到 [{to}] 共[{total}]条记录'
		}); 
		
		
		$("#executionMode").change(function(e) {
            if($(this).val() === "1")
			{
				$("#executionServerLi").css("display","none");
			}else
			{
				$("#executionServerLi").css("display","block");
			}
        });
		
		
		
		$('#executionMode').combobox({
			url: BASE_URL_DATA_DICTIONARY_VALUE_SEARCH +'/findByTypeCode?typeCode='+DDTC_KETTLE_JOB_EXEC_SERVER_TYPE +'&v=' + Math.random(),
			valueField: 'valueCode',
		    textField: 'valueName',
		    editable:false,
		    method:'get',
		    loadFilter:function(data){
		    	return data;
		    },
		    onSelect: function(record){
		    	var executionMethod = $('#executionMode').combobox('getText');
		    	if(executionMethod == '集群方式执行'){
		    		$('#executionServerLi').hide(); 
		    	}else{
		    		$('#executionServerLi').show(); 
		    	}
		    }
		});
		
		//执行服务器下拉框
		$('#executionServer').combobox({
			url: BASE_URL_KETTLE_SLAVE_SERVER_CRUD + '?v=' + Math.random(),
			valueField: 'id',
		    textField: 'hostName',
		    editable:false,
		    method:'get',
		    loadFilter:function(data){
		    	return data._embedded.kettleSlaveServers;
		    }
		    
		});
		

});

	function showWin(index){
		
		
		var row = $('#datagrid').datagrid('getRows')[parseInt(index, 10)];
		
    	if(row.serverForDisplay == '集群方式执行'){
    		InterfaceId = row.id;
    		$("#executionMode").combobox('setValue',row.remoteRunWay);
    		$('#executionServerLi').hide(); 
    	}else {
    		if(row.serverForDisplay == null){
	    		InterfaceId = row.id;
	    		var executionServerValues = $('#executionServer').combobox('getData');
	    		$('#executionServer').combobox('setValue',executionServerValues[0].id);
	    		$("#executionMode").combobox('setValue','2');
    		}else{
	    		InterfaceId = row.id;
	    		$("#executionMode").combobox('setValue',row.remoteRunWay/*'2'*/);
	    		var executionServerValues = $('#executionServer').combobox('getData');
	    		for(var i = 0;i <= executionServerValues.length;i++){
	    			if(executionServerValues[i].hostName == row.serverForDisplay){
	    				$('#executionServer').combobox('setValue',executionServerValues[i].id);
	    				break;
	    			}
    		}
    	}
    		$('#executionServerLi').show(); 
    	}
		$('#interfaceName').val(row.interfaceName);
		$('#win').window('open');
	}
	function rePage(){
		$('#win').window('close');
		$("#datagrid").datagrid("reload", BASE_URL_INTERFACE_SCHEDULE_SEARCH + '/findByExample' + '?v=' + Math.random());
	}
	
	//保存
	function onSave(){
		var name = InterfaceShceduingName;
		var runWay = $("#executionMode").combobox('getValue');
		var slaveServerId = $("#executionServer").combobox('getValue');
		var jsonData = {
				id:InterfaceId,
				runWay:runWay,
				slaveServerId:slaveServerId
		};
		
		var param = JSON.stringify(jsonData);
		if($("#ff").form("validate")){
			$("#instance_save").linkbutton("disable");
			$("#instance_save .l-btn-text").text("提交中...");
				$.ajax({
					url:BASE_URL_INTERFACE_SCHEDULE_CRUD +'/updateScheduleServer'  + '?v=' + Math.random(),
					type:'post',
					contentType:"application/json",
					data: param,
					success:function(){
						$.messager.alert('提示',"<p>修改成功!</p>","info");
						$('#win').window('close');
						$("#datagrid").datagrid("reload", BASE_URL_INTERFACE_SCHEDULE_SEARCH + '/findByExample' + '?v=' + Math.random());
						$("#instance_save").linkbutton("enable");
						$("#instance_save .l-btn-text").text("保存");
					},
					error:function(xhr,s,err){
						$.messager.alert("错误","<p>服务异常，修改失败，请重新登录或稍后再试！</p>","error");	
						$("#instance_save").linkbutton("enable");
						$("#instance_save .l-btn-text").text("保存");
					}
					
					
				});
			
		}
	}
	//查询
	function search(){
		$('#datagrid').datagrid('load' , BASE_URL_INTERFACE_SCHEDULE_SEARCH + '/findByExample' + '?v=' + Math.random());
	}